package com.zhiyou100.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import com.zhiyou100.entity.ResponseEntity;
import com.zhiyou100.entity.UserAddress;
import com.zhiyou100.entity.UserInfo;
import com.zhiyou100.service.inter.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户模块 地址簿
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-06 17:39
 */
@Controller
@ResponseBody
public class UserAddressController {
    @Autowired
    private UserAddressService service;

    @PutMapping("/user/address/add.action")
    public ResponseEntity<Object> addAddress(@RequestBody UserAddress address, HttpServletRequest request) {
        if (ObjectUtil.isNull(address)) {
            // 为 null
            return new ResponseEntity<>(500, "address 为空", null);
        }
        // 从域中获取
        final UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        // 设置 id
        address.setAddressUserId(userInfo.getUserId());
        // 插入
        service.insert(address);
        // 响应
        return new ResponseEntity<>(200, "添加成功", address);
    }

    @PostMapping("/user/address/update.action")
    public ResponseEntity<Object> updateAddress(@RequestBody UserAddress address, HttpServletRequest request) {
        if (ObjectUtil.isNull(address)) {
            // 为 null
            return new ResponseEntity<>(500, "address 为空", null);
        }
        // 从域中获取 当前用户
        final UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
        // 设置 id
        address.setAddressUserId(userInfo.getUserId());
        // 插入
        service.updateById(address);
        // 响应
        return new ResponseEntity<>(200, "修改成功", address);
    }

    @PostMapping("/user/address/delete.action")
    public ResponseEntity<Object> deleteAddress(@RequestParam("addressId") Integer id) {
        if (ObjectUtil.isNull(id)) {
            // 为 null
            return new ResponseEntity<>(500, "id 为空", null);
        }
        final UserAddress userAddress = service.selectById(id);
        if (ObjectUtil.isNull(userAddress)) {
            // 没有该地址
            return new ResponseEntity<>(500, "没有该地址", null);
        }
        service.deleteById(id);
        // 响应
        return new ResponseEntity<>(200, "删除成功", id);
    }


    @GetMapping("/user/address/select/list/{id}.action")
    public ResponseEntity<Object> deleteAddress(@PathVariable String id) {
        if (ObjectUtil.isNull(id)) {
            // 为 null
            return new ResponseEntity<>(500, "id 为空", null);
        }
        final List<UserAddress> userAddress = service.selectByMap(Dict.create()
                .set("address_user_id", id));
        if (ObjectUtil.isNull(userAddress)) {
            // 该 用户 ID 没有地址
            return new ResponseEntity<>(500, "此用户没有 地址簿", id);
        }
        // 响应
        return new ResponseEntity<>(200, "此用户的所有地址", userAddress);
    }
}
